﻿using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using DAL.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MqTools
{
    public class MQReceiver
    {
        public ConnectionFactory ConnectionFactory { get; private set; }

        public string Exchange { get; private set; }
        public string ExchangeType { get; private set; }
        public string HostName { get; private set; }
        public string VirtualHost { get; private set; }
        public string UserName { get; private set; }
        public string PassWord { get; private set; }
        public int Port { get; private set; }

        //public Action<string> Action { get; private set; }

        public IConnection Connection { get; private set; }
        public IModel Channel {get; private set;}       

        public MQReceiver(string hostName, string virtualHost, string userName, string password, string exchange,string exchangeType,int port=5672)
        {
            this.HostName = hostName;
            this.VirtualHost = virtualHost;
            this.UserName = userName;
            this.PassWord = password;
            this.Exchange = exchange;
            this.ExchangeType = exchangeType;

            ConnectionFactory = new ConnectionFactory()
            {

                VirtualHost = this.VirtualHost,
                UserName = this.UserName,
                Port = AmqpTcpEndpoint.UseDefaultPort,
                Password = this.PassWord,
                HostName = this.HostName
            };
            Connection = ConnectionFactory.CreateConnection();
            Channel = Connection.CreateModel();
        }

        //public void eventHandler(object mopdel, BasicDeliverEventArgs ea){
        //    var body = ea.Body;
        //    var message = Encoding.UTF8.GetString(body.ToArray());
        //    Console.WriteLine("收到消息 {0}", message);

        //}
        public void Bind(string queue, string route, Action<QueueMessage> action)
        {
         

            Channel.ExchangeDeclare(exchange: Exchange, ExchangeType, durable: true, autoDelete: false);
            Channel.QueueDeclare(queue: queue,
                                              durable: true,
                                              exclusive: false,
                                              autoDelete: false,
                                              arguments: null);
            Channel.QueueBind(queue: queue, exchange: Exchange, routingKey: route);
            var consumer = new EventingBasicConsumer(Channel);
      
            //consumer.Received += eventHandler;
            consumer.Received += (model, ea) =>
            {
                var message = Encoding.UTF8.GetString(ea.Body.ToArray());
                action.Invoke(JsonConvert.DeserializeObject<QueueMessage>(message));
            };
            Channel.BasicConsume(queue: queue,
                                     autoAck: true,
                                     consumer: consumer);


        }


       
    }
}